package com.scau.classOnline.service;

import com.scau.classOnline.dao.CommentMapper;
import com.scau.classOnline.dao.UserMapper;
import com.scau.classOnline.entity.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CommentService {
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    UserMapper userMapper;
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    CommentMapper commentMapper;

    static int FAVS = 7, FAVE = 10;

    public int addComment(int id, Integer cid, Integer stars, String content) {
        if (commentMapper.selectByCIdAndSId(cid, id) != null) {
            return -1;
        }
        Comment comment = new Comment();
        comment.setCid(cid);
        comment.setContent(content);
        comment.setSid(id);
        comment.setStar(stars);
        return commentMapper.insert(comment);
    }

    public List<Comment> getComments(Integer cid, Integer offset, Integer alter) {
        switch (alter) {
            case 0:
                return commentMapper.selectByCId(cid, offset);
            case 1:
                return commentMapper.selectByCIdBetweenStar(cid, offset, FAVS, FAVE);
            case 2:
                return commentMapper.selectByCIdBetweenStar(cid, offset, 0, FAVS);
            default:
                return null;
        }
    }

    public double computeFavRate(Integer cid) {

        int total = commentMapper.selectTotolNumByCId(cid);
        int fav = commentMapper.selectFavNumByCId(cid, FAVS, FAVE);
        return (double)fav / (double) total;
    }
}
